﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    class LichVanChuyenDAO :DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {


            LichVanChuyenDTO objectDTO = new LichVanChuyenDTO();
            objectDTO.MaLich = int.Parse(dt.Rows[i]["MaLich"].ToString());
            objectDTO.MaTuyen = int.Parse(dt.Rows[i]["MaTuyen"].ToString());
            objectDTO.TenLich = dt.Rows[i]["TenLich"].ToString();
            objectDTO.SoChuyenNgay = int.Parse(dt.Rows[i]["SoChuyenNgay"].ToString());
            objectDTO.GioVanChuyen = dt.Rows[i]["GioVanChuyen"].ToString();
            return (object)objectDTO;
        }

        public LichVanChuyenDTO[] GetAll()
        {
            String strSQL = "Select * from LichVanChuyen ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            LichVanChuyenDTO[] lichvanchuyenDTOs = new LichVanChuyenDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                lichvanchuyenDTOs[i] = (LichVanChuyenDTO)GetDataFromDataRow(dt, i);

            return lichvanchuyenDTOs;
        }

        public void insert(LichVanChuyenDTO info)
        {
            string insertCommand = "INSERT LichVanChuyen VALUES(" +
                info.MaLich + ", '" +
                info.MaTuyen + ", '" +
                info.TenLich + "', '" +
                info.SoChuyenNgay + "', '" +
                info.GioVanChuyen + "')";

            ExecuteNonQuery(insertCommand);
        }

        public void update(LichVanChuyenDTO info)
        {
            string updateCommand = "UPDATE LichVanChuyen " +
                                    "SET MaTuyen = '" + info.MaTuyen + "', " +
                                    " TenLich = '" + info.TenLich + "', " +
                                    " SoChuyenNgay = '" + info.SoChuyenNgay + "', " +
                                    " GioVanChuyen = '" + info.GioVanChuyen + "'" +
                                    " WHERE MaLich = " + info.MaLich;


            ExecuteNonQuery(updateCommand);
        }

        public void delete(string MaLich)
        {
            string deleteCommand = "DELETE FROM LichVanChuyen WHERE MaLich= " + MaLich;
            ExecuteNonQuery(deleteCommand);
        }
    }
}
